<?php
/**
 * @Author:      zy
 * @DateTime:    2017-09-26
 * @Description: 消息提醒
 */
class SceRemindController extends BaseController{

    /**
     * @Author:      zy
     * @DateTime:    2017-09-26 
     * @Description: 判断技师当天是否有新的消息提醒
     */
    public function is_remind(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',               
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }
            $token = Input::get('token'); //传入参数
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                $response = Lang::get("sce_error.user_token_not_exist_1004");
                break;
            }
            
            $start_date = date("Y-m-d 00:00:00");
            $end_date = date("Y-m-d 23:59:59");
            $info = Remind::where("remind_by",$engineer_data->id)
                    ->where("shop_id",$engineer_data->type_id)
                    ->where("user_type",2)
                    ->where("remind_at",">",$start_date)
                    ->where("remind_at","<",$end_date)
                    ->where("status",1)
                    ->select("id","remind_at","content","color")
                    ->get();
            $result = !empty($info->toarray()) ? 1 : 0; 
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>["result"=>$result]);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "is_remind", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zy
     * @DateTime:    2017-09-26 
     * @Description: 技师当天提醒列表
     */
    public function remind_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',               
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }
            $token = Input::get('token'); //传入参数
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                $response = Lang::get("sce_error.user_token_not_exist_1004");
                break;
            }
            
            $start_date = date("Y-m-d 00:00:00");
            $end_date = date("Y-m-d 23:59:59");
            $info = Remind::where("remind_by",$engineer_data->id)
                    ->where("shop_id",$engineer_data->type_id)
                    ->where("user_type",2)
                    ->where("remind_at",">",$start_date)
                    ->where("remind_at","<",$end_date)
                    ->where("status",1)
                    ->get()->toarray();
            $info_array = Common::remind_show_date($info);
            $response = array('code' => 0, 'msg' => 'ok', 'result' =>  array_values($info_array));
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "is_remind", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zy
     * @DateTime:    2017-09-26 
     * @Description: 技师所有消息提醒
     */
    public function all_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',               
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("sce_error.login_args_error_1001");
                break;
            }
            $token = Input::get('token'); //传入参数
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                $response = Lang::get("sce_error.user_token_not_exist_1004");
                break;
            }

            $info = Remind::where("remind_by",$engineer_data->id)
                    ->where("shop_id",$engineer_data->type_id)
                    ->where("user_type",2)
                    ->get()
                    ->toarray();
            $info_array = Common::remind_show_date($info);

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array_values($info_array));
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "is_remind", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
       /**
    * @Author:    zy
    * @DateTime:  2017-09-26
    * @Description: 关闭提醒
    */
    public function update_remind(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'remind_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            $token = Input::get('token'); //传入参数
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                $response = Lang::get("sce_error.user_token_not_exist_1004");
                break;
            }
            
            $Remind = Remind::find(Input::get("remind_id"));
            if ($Remind->remind_at>date("Y-m-d 23:59:59")) {
                $response = Lang::get("sce_error.detection_error_3001");
                break;
            }
            $Remind->status = 0;
            $result = $Remind->save() ? 1 : 0 ;
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ['result'=>$result]);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update_remind", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
}
